package com.cpr.core.nio.channel;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

/**
 * @author Chen Panrui
 * created at 2021/12/23 14:47
 */
public class NioClient {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1",8888));
        // 客户端向 server发送消息-基于channel的-基于Buffer(缓冲区的)
        socketChannel.write(ByteBuffer.wrap("还钱吧".getBytes(StandardCharsets.UTF_8)));
        // 客户端从 从server获取返回值-基于channel的-基于Buffer(缓冲区的)
        ByteBuffer allocate = ByteBuffer.allocate(1024);
        int read = socketChannel.read(allocate);
        System.out.println("服务端消息:" + new String(allocate.array(),0,read,StandardCharsets.UTF_8));
        socketChannel.close();
    }
}
